package com.yunbiao.communication;

import android.util.Log;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;

public class NotificationPacketListener implements PacketListener {

    private static final String LOGTAG = LogUtil.makeLogTag(NotificationPacketListener.class);

    private final XmppManager xmppManager;

    public NotificationPacketListener(XmppManager xmppManager) {
        this.xmppManager = xmppManager;
    }

    @Override
    public void processPacket(Packet packet) {
        Log.d(LOGTAG, "NotificationPacketListener.processPacket()...");
        Log.d(LOGTAG, "packet.toXML()=" + packet.toXML());

        if (packet instanceof NotificationIQ) {
            NotificationIQ notification = (NotificationIQ) packet;

            if (notification.getChildElementXML().contains("androidpn:iq:notification")) {
                String title = notification.getTitle();
                if (title != null && title.equals("base")) {
                    //处理消息更新处理
                    String message = notification.getMessage();
                    ServiceManager.getInstance().handleMessage(message);
                }
                //发送收到通知回执
                IQ result = NotificationIQ.createResultIQ(notification);
                try {
                    xmppManager.getConnection().sendPacket(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
